package com.fanix5.javamvvm.Ext;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.blankj.utilcode.util.ColorUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fanix5.javamvvm.R;
import com.fanix5.javamvvm.bean.ListDataUiState;
import com.fanix5.javamvvm.callback.EmptyCallback;
import com.fanix5.javamvvm.callback.ErrorCallback;
import com.fanix5.javamvvm.callback.LoadingCallback;
import com.fanix5.javamvvm.widget.DefineLoadMoreView;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.Transport;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.Collection;

import kotlin.jvm.functions.Function0;

public class CustomViewExt {

    private static CustomViewExt mInstance;

    public static CustomViewExt getInstance(){
        if(mInstance == null){
            synchronized (CustomViewExt.class){
                if(mInstance == null){
                    mInstance = new CustomViewExt();
                }
            }
        }
        return mInstance;
    }

    public void initSwipeRecyclerView(Activity activity, SwipeRecyclerView recyclerView, BaseQuickAdapter adapter,DefineLoadMoreView footerView,SwipeRecyclerView.LoadMoreListener loadMoreListener){
        recyclerView.setFocusableInTouchMode(false);
        recyclerView.requestFocus();
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setItemAnimator(null);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));

        footerView.setLoadViewColor(ColorUtils.getColor(R.color.colorPrimary));
        footerView.setmLoadMoreListener(loadMoreListener);
        recyclerView.addFooterView(footerView);
        recyclerView.setLoadMoreView(footerView);
        recyclerView.setLoadMoreListener(loadMoreListener);


    }

    public void initSwipeRefreshLayout(SwipeRefreshLayout swipeRefreshLayout){
        swipeRefreshLayout.setColorSchemeColors(ColorUtils.getColor(R.color.colorPrimary));
    }


    public void loadListData( ListDataUiState dataUiState, SwipeRecyclerView recyclerView, SwipeRefreshLayout swipeRefreshLayout, BaseQuickAdapter adapter, LoadService loadService){
        swipeRefreshLayout.setRefreshing(false);
        recyclerView.loadMoreFinish(dataUiState.isEmpty(),dataUiState.isHasMore());
        //请求成功
        if(dataUiState.isSuccess()){
            //第一页并没有数据 显示空布局界面
            if(dataUiState.isFirstEmpty()){
                showEmpty(loadService);
                //是第一页
            }else if(dataUiState.isRefresh()) {
                adapter.setList((Collection) dataUiState.getArrayList());
                loadService.showSuccess();
                //不是第一页
            }else {
                adapter.addData((Collection) dataUiState.getArrayList());
                loadService.showSuccess();
            }
            //请求失败
        }else {
            if(dataUiState.isRefresh()){
                //如果是第一页，则显示错误界面，并提示错误信息
                showError(loadService,dataUiState.getErrMessage());
            }else {
                recyclerView.loadMoreError(0,dataUiState.getErrMessage());
            }
        }

    }

    public void showLoad(LoadService loadService){
        loadService.showCallback(LoadingCallback.class);
    }

    public void showEmpty(LoadService loadService){
        loadService.showCallback(EmptyCallback.class);
    }

    public void showError(LoadService loadService,String msg){
        setErrorText(loadService,msg);
        loadService.showCallback(ErrorCallback.class);
    }

    public void setErrorText(LoadService loadService,String msg){
        if(!TextUtils.isEmpty(msg)){
            loadService.setCallBack(ErrorCallback.class, new Transport() {
                @Override
                public void order(Context context, View view) {
                    TextView mTvError = (TextView) view.findViewById(R.id.error_text);
                    mTvError.setText(msg);
                }
            });
        }

    }



}
